using System.Data;
using System.Data.SqlClient;
using System.Text;
using Customs.Components.KDT.SXXK.Info;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.KDT.SXXK
{
	public class SanPham : SanPhamInfo
	{
		//---------------------------------------------------------------------------------------------
		public void Load(SanPhamInfo entityInfo)
		{
			this._SoTiepNhan = entityInfo.SoTiepNhan;
			this._MaHaiQuan = entityInfo.MaHaiQuan;
			this._NamTiepNhan = entityInfo.NamTiepNhan;
			this._Ma = entityInfo.Ma;
			this._MaDoanhNghiep = entityInfo.MaDoanhNghiep;
			this._Ten = entityInfo.Ten;
			this._MaHS = entityInfo.MaHS;
			this._DVT_ID = entityInfo.DVT_ID;
			this._SoThuTuHang = entityInfo.SoThuTuHang;
		}

		//---------------------------------------------------------------------------------------------
		public void InsertTransaction(SqlTransaction transaction, SanPhamInfoCollection collection)
		{
			SanPham sp = new SanPham();
			foreach (SanPhamInfo itemInfo in collection)
			{
				sp.Load(itemInfo);
				sp.InsertTransaction(transaction);
			}						
		}

		//---------------------------------------------------------------------------------------------
		public void InsertTransaction(SqlTransaction transaction)
		{
			StringBuilder query = new StringBuilder(); 
			query.Append("INSERT INTO DSPDK_D ( ");
			query.Append("So_TNDKDT, ");
			query.Append("Ma_HQTN, ");
			query.Append("Nam_TN, ");
			query.Append("MA_SP, ");
			query.Append("Ma_DV, ");
			query.Append("TEN_SP, ");
			query.Append("MA_HS, ");
			query.Append("MA_DVT, ");
			query.Append("STTHang ) ");
			
			query.Append("VALUES ( ");
			
			query.Append("@So_TNDKDT, ");
			query.Append("@Ma_HQTN, ");
			query.Append("@Nam_TN, ");
			query.Append("@MA_SP, ");
			query.Append("@Ma_DV, ");
			query.Append("@TEN_SP, ");
			query.Append("@MA_HS, ");
			query.Append("@MA_DVT, ");
			query.Append("@STTHang )");
			
			SqlParameter[] parameters = new SqlParameter[9];

			parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
			parameters[0].Value = this._SoTiepNhan;

			parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
			parameters[1].Value = this._MaHaiQuan;

			parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
			parameters[2].Value = this._NamTiepNhan;

			parameters[3] = new SqlParameter("@MA_SP", SqlDbType.VarChar, 30);
			parameters[3].Value = this._Ma;

			parameters[4] = new SqlParameter("@Ma_DV", SqlDbType.VarChar, 14);
			parameters[4].Value = this._MaDoanhNghiep;

			parameters[5] = new SqlParameter("@TEN_SP", SqlDbType.NVarChar, 80);
			parameters[5].Value = this._Ten;

			parameters[6] = new SqlParameter("@MA_HS", SqlDbType.VarChar, 12);
			parameters[6].Value = this._MaHS;

			parameters[7] = new SqlParameter("@MA_DVT", SqlDbType.Char, 3);
			parameters[7].Value = this._DVT_ID;

			parameters[8] = new SqlParameter("@STTHang", SqlDbType.Int);
			parameters[8].Value = this._SoThuTuHang;
			
			SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
		}

        //---------------------------------------------------------------------------------------------
        public int Delete(SqlTransaction transaction)
        {
            string query = string.Format("DELETE FROM DSPDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3} AND MA_SP = '{4}'", this._SoTiepNhan, this.MaHaiQuan.PadRight(6), this._MaDoanhNghiep, this._NamTiepNhan, this._Ma);
            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }

        //-----------------------------------------------------------------------------------------
        public static void Delete(SqlTransaction transaction, long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan)
        {
            string query = string.Format("DELETE FROM DSPDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3}", soTiepNhan, maHaiQuan.PadRight(6), maDoanhNghiep, namTiepNhan);
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }
	}
}
